/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package security.beans;

import Entity.Pair;
import Entity.Profile;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import org.primefaces.event.RowEditEvent;
import security.business.ProfileBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean (name = "createProfile")
@RequestScoped
public class CreateProfileBean {
    
    private List<Pair> list;
    Pair pair;
    private String profile;
    Connection conn;
    
    public CreateProfileBean() {
        FacesContext context 
                = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session 
                = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean 
                =(ConectionBean) session.getAttribute("conectionBean");
        
        list = new ArrayList<>();
        profile = "C##";
        conn = conectionBean.currentConnection; 
        init( conn );
    }

    public List<Pair> getList() {
        return list;
    }

    public void setList(List<Pair> list) {
        this.list = list;
    }

    public Pair getPair() {
        return pair;
    }

    public void setPair(Pair pair) {
        this.pair = pair;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }
    
    
    private void init(Connection connection) {
        FacesMessage msg = new FacesMessage();
        
        String reso[] = {"SESSIONS_PER_USER", "CPU_PER_SESSION",
                         "CPU_PER_CALL", "CONNECT_TIME",
                         "IDLE_TIME", "LOGICAL_READS_PER_SESSION",
                         "LOGICAL_READS_PER_CALL", "COMPOSITE_LIMIT",
                         "PRIVATE_SGA", "FAILED_LOGIN_ATTEMPTS",
                         "PASSWORD_LIFE_TIME", "PASSWORD_REUSE_TIME",
                         "PASSWORD_REUSE_MAX", "PASSWORD_LOCK_TIME",
                         "PASSWORD_GRACE_TIME", "PASSWORD_VERIFY_FUNCTION"};
        String val[] = {"2", "10000", "1", "UNLIMITED", "30", "DEFAULT",
                        "DEFAULT", "DEFAULT", "20M", "3", "30", "12",
                        "UNLIMITED", "DEFAULT", "2", "NULL"};
        
        for(int i=0; i< 16; i++){
            list.add( new Pair(reso[i], val[i] ) );
        }
    }
    
    
    public void onEdit(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Profile Edited", /*((Car) event.getObject()).getModel()*/"...");  
        System.out.print("EDIT!!!!! >>>");
        pair = ( (Pair) event.getObject());
        System.out.println("NAME: "+pair.getName()+" VAL: "+pair.getValue() );
        Pair ob;
        
        ob = new Pair( pair.getName(), pair.getValue() );
        
        /*if ( !ob.setValue( pair.getVariableName(), gb.getVariableValue() ) ){
            System.out.println("NO GOOD");
            onCancel(event);
            return;
        }*/
        System.out.println("GOOD");
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
      
    public void onCancel(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Profile Cancelled", /*((Car) event.getObject()).getModel()*/"...");  
        System.out.println("Cancel");
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
    
    public void create(){
        if( !profile.equals("") && !profile.equals("C##") ){
            FacesMessage msg ;
            ProfileBusiness pb = new ProfileBusiness( conn );
            try {
                String res= pb.create( profile, list);

                msg = new FacesMessage("¡Perfil Creado!", res);  
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                FacesContext.getCurrentInstance().addMessage(null, msg);  
            } catch (SQLException ex) {
                msg = new FacesMessage("Error al crear Perfil", ex.getMessage());  
                msg.setSeverity(FacesMessage.SEVERITY_FATAL);
                FacesContext.getCurrentInstance().addMessage(null, msg);  
            }
            
        }
    }
}
